package weaver.interfaces.zwgk.action;

import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zwgk.utils.WebServiceUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-09-12
 * @Description: 请购单流程调用请购单创建/核检接口
 * @ModifyLog:
 **/

public class WeaqgddAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = new RecordSet();
        writeLog( ">>>>>>WeaqgddCreateAction>>>>>" + requestid );
        try {
            Map<String, String> masterMap = new HashMap<>();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String qgdh = Util.null2String(rs.getString("qgdh"));
            String qgrq = Util.null2String(rs.getString("qgrq"));
            String ckgysbh = Util.null2String(rs.getString("ckgysbh"));
            String sqrgh = Util.null2String(rs.getString("sqrgh"));
            String sqbm = Util.null2String(rs.getString("qgbmbm"));
            String fktj = Util.null2String(rs.getString("fktj"));
            String sz = Util.null2String(rs.getString("sz"));
            String bz = Util.null2String(rs.getString("bz"));
            String khmc = Util.null2String(rs.getString("khmc"));
            String jkmc = Util.null2String(rs.getString("jkmc"));
            masterMap.put("pmk01", qgdh);
            masterMap.put("pmk04", qgrq);
            masterMap.put("pmk09", ckgysbh);
            masterMap.put("pmk02", "CAP");
            masterMap.put("pmk20", fktj);
            masterMap.put("pmk12", sqrgh);
            masterMap.put("pmk13", sqbm);
            masterMap.put("pmk21", sz);
            masterMap.put("pmk22", bz);
            masterMap.put("pmkud02", khmc);
            writeLog(">>>>>>>>>>>>主表字段信息=" + masterMap.toString()+"<br/>");
            List<Map<String, String>> detailMapList = new ArrayList<>();
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?  ", mainid);
            int count = 1;
            while (rs.next()) {
                Map<String, String> map = new HashMap<>();
                map.put("pml02", count + "");
                map.put("pml04", Util.null2String(rs.getString("ljbh")));
                map.put("pml041", Util.null2String(rs.getString("pm")));
                map.put("pml33", Util.null2String(rs.getString("jhrq")));
                map.put("pml34", Util.null2String(rs.getString("dcrq")));
                map.put("pml35", Util.null2String(rs.getString("dkrq")));
                map.put("pml07", Util.null2String(rs.getString("qgdw")));
                map.put("pml08", Util.null2String(rs.getString("kcdw")));
                map.put("pml20", Util.null2String(rs.getString("dgl")));
                map.put("pmlud01", Util.null2String(rs.getString("zldj")));
                map.put("pmlud06", Util.null2String(rs.getString("xplp")));
                writeLog(">>>>>>>>>>>>明细字段信息=" + map.toString()+"<br/>");
                detailMapList.add(map);
                count++;
            }
            if ("".equals( jkmc )) {
                requestInfo.getRequestManager().setMessageid( "111111" );
                requestInfo.getRequestManager().setMessagecontent( "接口返回异常信息：接口名称不能为空" );
                return Action.FAILURE_AND_CONTINUE;
            }
            JSONObject resultJson = WebServiceUtil.Apmt420Work(jkmc, masterMap, detailMapList);
            writeLog(">>>>>>>>>>>>接口返回结果=" + resultJson.toString());
            String code = Util.null2String( resultJson.getString( "code" ) );
            String description = Util.null2String( resultJson.getString( "description" ) );
            if (!"0".equals( code )) {
                requestInfo.getRequestManager().setMessageid( "111111" );
                requestInfo.getRequestManager().setMessagecontent( "接口返回异常信息：" + description );
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }

    }
}
